package de.tabuma.shopping.controller;

import de.tabuma.shopping.*;
import de.tabuma.shopping.entity.*;
import de.tabuma.shopping.logik.*;
import de.tabuma.lib.tabumacontroller;
import java.io.*;
import java.util.*;
import java.util.logging.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.google.appengine.api.users.*;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.2 $
 * @since $Date: 2009/07/21 19:37:32 $
 */
public class licensePackageOverview extends tabumacontroller {

    private static Logger log =  Logger.getLogger(licensePackageOverview.class.getName());

    LicensePackageFacade licensePackageFacade;

    String orderNav;
    public licensePackageOverview(String servlet, Hashtable parameter, String orderNav, LicensePackageFacade licensePackageFacade) {
        super(servlet, parameter);
        this.orderNav = orderNav;
        this.licensePackageFacade = licensePackageFacade;
    }

    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response,PrintWriter out)  {
        try {
            UserService userService = UserServiceFactory.getUserService();
            User user = userService.getCurrentUser();

            List<LicensePackage> list = licensePackageFacade.getCurrentLicensePackageList();
            request.setAttribute(de.tabuma.shopping.constants.ORDER_CURRENT_LICENSEPACKAGE_LIST, list);
            CurrentOrder currentOrder = CurrentOrderFacade.getCurrentOrder(user.getEmail());
            request.setAttribute(de.tabuma.shopping.constants.ORDER_CURRENT, currentOrder);
            List<CurrentOrderLicensePackageRelation> colprs = CurrentOrderLicensePackageRelationFacade.getList(currentOrder.getId().intValue());
            request.setAttribute(constants.ORDER_CURRENT_LICENSEPACKAGERELATION_LIST, colprs);
            request.setAttribute(orderNav,de.tabuma.shopping.constants.ORDERNAV_PACKAGE);

            if(request.getParameter("minusLP")!=null || request.getParameter("minusLP.y")!=null) {
                log.fine("Minus was clicked.");
                return;
            }
            if(request.getParameter("minusOrderElement")!=null || request.getParameter("minusOrderElement.y")!=null) {
                log.fine("Minus in Order List was clicked.");
                return;
            }
            if(request.getParameter("removeOrderAll")!=null || request.getParameter("removeOrderAll.y")!=null) {
                log.fine("Remove all was clicked in order list.");
                CurrentOrderFacade.removeAllPackageFromCurrentOrder(user.getEmail());
                colprs = CurrentOrderLicensePackageRelationFacade.getList(currentOrder.getId().intValue());
                request.setAttribute(constants.ORDER_CURRENT_LICENSEPACKAGERELATION_LIST, colprs);
                return;
            }
            if(request.getParameter("startorder")!=null) {
                log.fine("Start order was clicked.");
                request.setAttribute(orderNav,de.tabuma.shopping.constants.ORDERNAV_LICENSEAGREEMENT);
                log.fine("Setting "+orderNav +" to "+de.tabuma.shopping.constants.ORDERNAV_LICENSEAGREEMENT);
                return;
            }

            //Step 1 Handling the "add" licensepackage to order list events
            Iterator<LicensePackage> myIt = list.iterator();
            while(myIt.hasNext()) {
                LicensePackage lp = myIt.next();
                if(request.getParameter("LicensePackage"+lp.getId())!=null || request.getParameter("LicensePackage"+lp.getId()+".y")!=null) {
                    CurrentOrderFacade.addLicensePackageToCurrentOrder(user.getEmail(),(int) lp.getId().longValue());
                    log.fine("LicensePackage with ID "+lp.getId()+" was added to order objekt.");
                    colprs = CurrentOrderLicensePackageRelationFacade.getList(currentOrder.getId().intValue());
                    request.setAttribute(constants.ORDER_CURRENT_LICENSEPACKAGERELATION_LIST, colprs);
                    return;
                }


                if(request.getParameter("plusLP"+lp.getId())!=null || request.getParameter("plusLP"+lp.getId()+".y")!=null) {
                    log.fine("Plus clicked for LicensePackage ID "+lp.getId()+".");
                    request.setAttribute("plusLP"+lp.getId(),"true");
                    log.fine("Setting "+orderNav +" to "+de.tabuma.shopping.constants.ORDERNAV_PACKAGE);
                    return;
                }

            }

            //Handling the
            currentOrder = CurrentOrderFacade.getCurrentOrder(user.getEmail());
            request.setAttribute(de.tabuma.shopping.constants.ORDER_CURRENT, currentOrder);

            colprs = CurrentOrderLicensePackageRelationFacade.getList(currentOrder.getId().intValue());
            request.setAttribute(constants.ORDER_CURRENT_LICENSEPACKAGERELATION_LIST, colprs);
            for(int i=0;i<colprs.size();i++) {
                if(request.getParameter("plusOrderElement"+i)!=null || request.getParameter("plusOrderElement"+i+".y")!=null) {
                    log.fine("Plus was clicked in number "+i+" in order list.");
                    request.setAttribute("plusOrderElement"+i,"true");
                    return;
                }
                if(request.getParameter("removeOrderID"+i)!=null || request.getParameter("removeOrderID"+i+".y")!=null) {
                    log.fine("Remove was clicked in number "+i+" in order list.");
                    request.setAttribute(orderNav,de.tabuma.shopping.constants.ORDERNAV_PACKAGE);
                    CurrentOrderFacade.removeLicensePackageFromCurrentOrder(user.getEmail(), i);
                    colprs = CurrentOrderLicensePackageRelationFacade.getList(currentOrder.getId().intValue());
                    request.setAttribute(constants.ORDER_CURRENT_LICENSEPACKAGERELATION_LIST, colprs);
                    return;
                }
            }
        }
        catch(Exception e) {
            log.warning(e.getMessage());
        }
    }
}
